package net.neiquan.applibrary.wight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import net.neiquan.applibrary.utils.PixelUtil;

public class MyProgressArc extends View {
    private int width;//设置高
    //private int height;//设置高
    //设置画笔
    private Paint mPaintBackground;
    private Paint mPaintCurrent;
    private Paint mPaintArc;
    private float centerX;
    private float centerY;
    //设置进度
    private int maxProgress = 100;
    private int currentProgress = 0;
    private Context mContext;

    public int getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }

    public int getCurrentProgress() {
        return currentProgress;
    }

    public void setCurrentProgress(int currentProgress) {
        this.currentProgress = currentProgress;
        invalidate();//实时更新进度
    }

    public MyProgressArc(Context context) {
        super(context);
        mContext = context;
    }

    public MyProgressArc(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        int pix10 = PixelUtil.dp2px(context, 10);
        mPaintBackground = new Paint();
        mPaintBackground.setAntiAlias(true);
        mPaintBackground.setStyle(Paint.Style.STROKE);
        mPaintBackground.setColor(Color.LTGRAY);
        mPaintBackground.setStrokeWidth(pix10);


        mPaintArc = new Paint();
        mPaintArc.setAntiAlias(true);
        mPaintArc.setStyle(Paint.Style.STROKE);
        mPaintArc.setColor(Color.rgb(241, 139, 40));
        mPaintArc.setStrokeWidth(pix10);

        mPaintCurrent = new Paint();
        mPaintCurrent.setAntiAlias(true);
        mPaintCurrent.setStyle(Paint.Style.FILL);
        mPaintCurrent.setColor(Color.rgb(241, 139, 40));
        mPaintCurrent.setStrokeWidth(pix10);

      /*  mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setColor(Color.BLACK);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mPaintText.setTextSize(0);*/
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        //  height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        centerX = (width) / 2;
        centerY = (width) / 2;
        int pix20 = PixelUtil.dp2px(mContext, 10);
        setMeasuredDimension(width + pix20, width + pix20);//设置宽和高
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int pix = PixelUtil.dp2px(mContext, 100);
        RectF rect = new RectF(width / 2 - pix, width / 2 - pix, width / 2 + pix, width / 2 + pix);
        canvas.drawArc(rect, currentProgress * 360f / maxProgress - 90, 360f - currentProgress * 360f / maxProgress, false, mPaintBackground);
        canvas.drawArc(rect, -90, currentProgress * 360f / maxProgress, false, mPaintArc);
        int pix5 = PixelUtil.dp2px(mContext, 5);
        float centerX0 = (float) (centerX + (pix) * Math.cos((currentProgress * 360f / maxProgress - 90) * 3.14 / 180));
        float centerY0 = (float) (centerX + (pix) * Math.sin((currentProgress * 360f / maxProgress - 90) * 3.14 / 180));
        int pix15 = PixelUtil.dp2px(mContext, 13);
        canvas.drawCircle(centerX0, centerY0, pix15, mPaintCurrent);
    }

}